package com.ls.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.TimeUnit;

public class NIOMultiThreadClient {


    public static void main(String[] args) throws IOException, InterruptedException {
        int n = 20;

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8088));


        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        for(int i = 0; i < n; i++){
            final int index = i;
            String msg = "hello" + index;
            byteBuffer.put(msg.getBytes());
            if(socketChannel.finishConnect()){
                byteBuffer.flip();
                socketChannel.write(byteBuffer);
            }
            byteBuffer.clear();
        }

        new Thread(() ->{
            ByteBuffer readBuf = ByteBuffer.allocateDirect(1024);
            while(true){
                try {
                    int num = socketChannel.read(readBuf);
                    if(num > 0){
                        readBuf.flip();
                        byte[] bytes = new byte[readBuf.limit()];
                        readBuf.get(bytes);
                        System.out.println("response:" + new String(bytes));
                    }

                    readBuf.clear();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }


    public static void aa(){
        int n = 20;
        for(int i = 0; i < n; i++){
            final int index = i;
            new Thread(() ->{
                try {
                    SocketChannel socketChannel = SocketChannel.open();
                    socketChannel.configureBlocking(true);

                    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
                    String msg = "hello" + index;
                    byteBuffer.put(msg.getBytes());


                    socketChannel.connect(new InetSocketAddress("127.0.0.1", 8088));
                    if(socketChannel.finishConnect()){
                        byteBuffer.flip();
                        socketChannel.write(byteBuffer);

                        byteBuffer.clear();

                        int num = socketChannel.read(byteBuffer);
                        byteBuffer.flip();
                        byte[] bytes = new byte[byteBuffer.limit()];
                        byteBuffer.get(bytes);
                        System.out.println("response:" + new String(bytes));

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }).start();
        }
    }

}
